package common.message
{
	import com.greensock.TweenMax;
	
	import common.GlobalSet;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.setTimeout;
	
	import kernel.application.ApplicationDisplay;
	import common.AppLayers;
	import kernel.core.EnterFrameEventManager;
	import kernel.events.EnterFrameEvent;

	public class MiddleMessage extends Sprite
	{
		private var _txt:TextField;
		private var _tf:TextFormat;
		private var _count:uint=0;
		private var _showTime:uint;
		private var _moveUp:Number = 6.5;
		
		public function MiddleMessage(message:String,showTime:uint=3000,color:uint=0xFFCC33,size:uint=16)
		{
			this.mouseEnabled = false;
			_showTime = showTime;
			_txt = new TextField();
			_tf = new TextFormat();
			_tf.size = size;
			_tf.color = color;
			_tf.bold = true;
			_tf.align = TextFormatAlign.CENTER;
			_txt.multiline = true;
			_txt.wordWrap = true;
			_txt.width = ApplicationDisplay.stage.stageWidth*0.6;
			_txt.height = 300;
			_txt.defaultTextFormat = _tf;
			_txt.htmlText = message;
			_txt.width = _txt.textWidth +5;
			_txt.height = _txt.textHeight +3;
			_txt.x = int(-_txt.width/2);
			_txt.selectable = false;
			_txt.filters = GlobalSet.defaultTextFilter;
			this.addChild(_txt);
			this.x = int(ApplicationDisplay.stage.stageWidth/2);
			this.y = int(ApplicationDisplay.stage.stageHeight*0.4);
			AppLayers.noticeLayer.addChild(this);
			EnterFrameEventManager.add(goUp);
			
			var dis:DisplayObject;
			for(var i:int=0;i<this.parent.numChildren;i++)
			{
				dis = this.parent.getChildAt(i);
				if(dis is MiddleMessage)
				{
					if(dis != this)
					{
						TweenMax.to(dis,1.0,{alpha:0});
					}
				}
			}
			
		}
		private function goUp(event:EnterFrameEvent):void
		{
//			trace(event.timer,event.passTime,event.fps);
			
			this.y -= _moveUp;
			if(_moveUp>1)
			{
				_moveUp -= 0.5;
			}
			this.y = int(this.y);
			_count++;
			if(_count>15)
			{
				EnterFrameEventManager.remove(goUp);
				setTimeout(hideMe,_showTime)
			}
		}
		private function hideMe():void
		{
			ApplicationDisplay.stage.addEventListener(Event.ENTER_FRAME,alphaHide);
		}
		private function alphaHide(event:Event):void
		{
			this.alpha -= 0.2;
			this.y -= 1;
			if(this.alpha<=0)
			{
				ApplicationDisplay.stage.removeEventListener(Event.ENTER_FRAME,alphaHide);
				this.parent.removeChild(this);
				_txt.htmlText = "";
				_txt = null;
				_tf = null;
			}
		}
	}
}